package com.liang.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.base.Preconditions;
import com.liang.subject.application.convert.SubjectAnswerDTOConverter;
import com.liang.subject.application.convert.SubjectInfoDTOConverter;
import com.liang.subject.application.dto.SubjectInfoDTO;
import com.liang.subject.common.entity.PageDTO;
import com.liang.subject.common.entity.Result;
import com.liang.subject.domain.entity.SubjectAnswerBO;
import com.liang.subject.domain.entity.SubjectInfoBO;
import com.liang.subject.domain.service.SubjectInfoDomainService;
import com.liang.subject.infra.es.entity.SubjectInfoEs;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/subject")
public class SubjectInfoController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 新增题目
     */
    @PostMapping("/add")
    public Result<Boolean> save(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            log.info("SubjectInfoController.add.dto:{}", JSON.toJSONString(subjectInfoDTO));

            Preconditions.checkNotNull(subjectInfoDTO.getSubjectName(), "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分数不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds()), "分类id不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds()), "标签id不能为空");

            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTO2BO(subjectInfoDTO);
            List<SubjectAnswerBO> optionList = SubjectAnswerDTOConverter.INSTANCE.convertListDTOToBO(subjectInfoDTO.getOptionList());
            subjectInfoBO.setOptionList(optionList);
            subjectInfoDomainService.save(subjectInfoBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增题目失败");
        }
    }

    /**
     * 查询题目列表
     */
    @PostMapping("/querySubjectList")
    public Result<PageDTO<SubjectInfoDTO>> querySubjectList(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            log.info("SubjectInfoController.querySubjectList.dto:{}", JSON.toJSONString(subjectInfoDTO));

            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "标签id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类id不能为空");
//            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空");

            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTO2BO(subjectInfoDTO);
            PageDTO<SubjectInfoBO> pageBO = subjectInfoDomainService.querySubjectList(subjectInfoBO);
            PageDTO<SubjectInfoDTO> pageDTO = SubjectInfoDTOConverter.INSTANCE.convertBO2DTO(pageBO);
            return Result.ok(pageDTO);
        } catch (Exception e) {
            log.error("SubjectInfoController.querySubjectList.error:{}", e.getMessage(), e);
            return Result.fail("查询题目列表失败");
        }
    }

    /**
     * 查询题目详情
     */
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            log.info("SubjectInfoController.querySubjectInfo.dto:{}", JSON.toJSONString(subjectInfoDTO));

            Preconditions.checkNotNull(subjectInfoDTO.getId(), "题目id不能为空");

            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTO2BO(subjectInfoDTO);
            SubjectInfoBO bo = subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDTO dto = SubjectInfoDTOConverter.INSTANCE.convertBO2DTO(bo);
            return Result.ok(dto);
        } catch (Exception e) {
            log.error("SubjectInfoController.querySubjectInfo.error:{}", e.getMessage(), e);
            return Result.fail("查询题目详情失败");
        }
    }

    /**
     * 全文检索
     */
    @PostMapping("/getSubjectPageBySearch")
    public Result<PageDTO<SubjectInfoEs>> getSubjectPageBySearch(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPageBySearch.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkArgument(StringUtils.isNotBlank(subjectInfoDTO.getKeyword()), "关键词不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTO2BO(subjectInfoDTO);
            subjectInfoBO.setPageNum(subjectInfoDTO.getPageNum());
            subjectInfoBO.setPageSize(subjectInfoDTO.getPageSize());
            PageDTO<SubjectInfoEs> boPageResult = subjectInfoDomainService.getSubjectPageBySearch(subjectInfoBO);
            return Result.ok(boPageResult);
        } catch (Exception e) {
            log.error("SubjectCategoryController.getSubjectPageBySearch.error:{}", e.getMessage(), e);
            return Result.fail("全文检索失败");
        }
    }

    /**
     * 获取题目贡献榜
     */
    @PostMapping("/getContributeList")
    public Result<List<SubjectInfoDTO>> getContributeList() {
        try {
            List<SubjectInfoBO> list =subjectInfoDomainService.getContributeList();
            List<SubjectInfoDTO> res = SubjectInfoDTOConverter.INSTANCE.convert2DTOList(list);
            return Result.ok(res);
        } catch (Exception e) {
            log.error("SubjectCategoryController.getContributeList.error:{}", e.getMessage(), e);
            return Result.fail("获取题目贡献榜失败");
        }
    }

    /**
     * 测试MQ发送
     */
    @PostMapping("/pushMessage")
    public Result<Boolean> pushMessage(@Param("id") Integer id) {
        rocketMQTemplate.convertAndSend("first-topic", "姬霓太美" + id);
        log.info("SubjectInfoController.pushMessage.id:{}", id);
        return Result.ok(true);
    }
}
